package Exercise;

import java.util.Random;
import java.util.Scanner;


public class Test3UnionLotto {

    //生成红球和蓝球的数字
    public static int[] ballNum() {
        Random r = new Random();
        int[] balls = new int[7];
        for (int i = 0; i < balls.length - 1; i++) {
            do {
                balls[i] = r.nextInt(33) + 1;
            } while (judge(balls, balls[i]));
        }
        balls[balls.length - 1] = r.nextInt(16) + 1;
        return balls;
    }

    //测试生成、输入的号码是否有重复
    public static boolean judge(int[] arr, int num) {
        for (int i = 0; i < arr.length - 1; i++)
            if (num == arr[i])
                return false;
        return true;
    }

    //输入数字
    public static int[] guessNum() {
        Scanner sc = new Scanner(System.in);
        int[] arr = new int[7];
        int temp;
        for (int i = 0; i < 6; ) {
            System.out.print("红球" + (i + 1) + ":>");
            temp = sc.nextInt();
            if (judge(arr, temp) && (temp >= 1) && (temp <= 33)) {
                arr[i] = temp;
                i++;
            } else
                System.out.println("数字不在规定范围内或有重复，请重新输入");
        }
        System.out.print("蓝球:>");
        while (true) {
            temp = sc.nextInt();
            if (temp >= 1 && temp <= 16) {
                arr[arr.length - 1] = temp;
                break;
            } else
                System.out.println("数字不在规定范围内，请重新输入");
        }
        return arr;
    }

    //计算猜对的球的个数，并将其储存与数组中返回
    public static int[] judgeinput(int[] arr1, int[] arr2) {
        int count = 0;
        int[] arr = new int[2];
        for (int i = 0; i < arr1.length - 1; i++) {
            if (arr1[i] == arr2[i])
                count++;
        }
        arr[0] = count;
        if (arr1[arr1.length - 1] == arr2[arr1.length - 1])
            arr[1] = 1;
        return arr;
    }

    //根据所给数据计算几等奖
    public static void judge2(int[] result) {
        if (result[1] == 1 && (result[0] == 0 || result[0] == 1 || result[0] == 2))
            System.out.print("恭喜你中了六等奖");
        else if ((result[1] == 1 && result[0] == 3) || (result[1] == 0 && result[0] == 4))
            System.out.print("恭喜你中了五等奖");
        else if ((result[1] == 1 && result[0] == 4) || (result[1] == 0 && result[0] == 5))
            System.out.print("恭喜你中了四等奖");
        else if (result[1] == 1 && result[0] == 5)
            System.out.print("恭喜你中了三等奖");
        else if ((result[1] == 0) && result[0] == 6)
            System.out.print("恭喜你中了二等奖");
        else if (result[1] == 1 && result[0] == 6)
            System.out.print("恭喜你中了一等奖");
        else
            System.out.print("很遗憾，你没有中奖");
    }

    public static void main(String[] args) {
        int[] ballNum = ballNum();
        int[] guessNum = guessNum();
        judge2(judgeinput(ballNum, guessNum));
    }
}